package xyz.chaobei.feature.interfaces;

import xyz.chaobei.feature.entity.Person;
import xyz.chaobei.feature.factory.PersonFactory;

import java.util.Arrays;
import java.util.List;

/**
 * @author <a href='mailto:maruichao52@gmail.com'>MRC</a>
 * @since 2021/2/3
 */
public class Main {

    private static int desc;

    public static void main(String[] args) {

        /**
         * 实现接口的默认方法
         */
        DefaultInterface feature = new DefaultInterface() {
            @Override
            public int add(int a, int b) {
                return a + b;
            }
        };
        System.out.println(feature.add(1, 2));
        System.out.println(feature.sub(4, 2));

        System.out.println("------------------------------------");

        // lambda 表达式
        List<String> str = Arrays.asList("desc", "abc", "fac", "page");
        /**
         * Collections.sort(str, (String a, String b) -> a.compareTo(b))
         * 更加简洁的方式
         */
        str.sort((a, b) -> a.compareTo(b));
        System.out.println(str);

        System.out.println("------------------------------------");

        // 函数式接口,适用于加了注释，并且只有一个抽象方法
        FunctionInterface<String, Integer> function = (val) -> Integer.valueOf(val);
        Integer result = function.cover("12");
        System.out.println(result);
        System.out.println(result.getClass().toString());

        System.out.println("------------------------------------");


        /**
         *     静态方法引用
         *     public static Integer valueOf(String s) throws NumberFormatException {
         *         return Integer.valueOf(parseInt(s, 10));
         *     }
         */
        FunctionInterface<String, Integer> function2 = Integer::valueOf;
        Integer result2 = function.cover("123");
        System.out.println(result2);
        System.out.println(result2.getClass().toString());
        System.out.println("------------------------------------");

        /**
         * 引用对象的方法
         */
        Desc desc = new Desc();
        FunctionInterface<Integer, String> function3 = desc::valueOf;
        String result3 = function3.cover(Integer.valueOf(456));
        System.out.println(result3);
        System.out.println(result3.getClass().toString());
        System.out.println("------------------------------------");

        /**
         * 引用new 关键字的构造方法
         */
        PersonFactory<Person> personFactory = Person::new;
        Person person = personFactory.create("mmm", 12);
        System.out.println(person.toString());

        System.out.println("------------------------------------");

        /**
         * lambda 中使用的局部变量具有final语义，即不可修改
         */
        final int num = 1;
        FunctionInterface<String, Integer> function4 = (val) -> Integer.valueOf(val + num);
        Integer result4 = function4.cover("12");

        System.out.println(result4);
        System.out.println(result4.getClass().toString());

        System.out.println("------------------------------------");

    }
    /**
     * <p>在类的成员方法中，可以访问类的成员变量。
     * <p>author: <a href='mailto:maruichao52@gmail.com'>MRC</a>
     * @since 2021/2/3
     * @return void
     **/
    public void testScope() {
        FunctionInterface<String,Integer> function6 = new FunctionInterface<String, Integer>() {
            @Override
            public Integer cover(String t) {
                desc = 0;
                return null;
            }
        };
    }


}
